<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Peron_model
 *
 * @author loind
 */
class Item_model extends CI_Model {

    var $name = '';
    var $company = '';
    var $email = '';
    var $age = '';
    var $address = '';
    var $linkin = '';
    var $description = '';
    var $rating = '';
    var $avatar = 'default.png';
    var $category_Id = '';
    var $user_id = '';
    var $session_id = '';
   
    //put your code here
    public function __construct() {
        parent::__construct();
        
    }

    function get_top_five_entries() {
        $query = $this->db->get('items',5);
        return $query->result();
    }
    function get_top_five_entries_by_category_Id($category_Id='0') {
        $conditions = array('category_Id'=> $category_Id);
        $this->db->where($conditions);
        $query = $this->db->get('items', 5);
        return $query->result();
    }

    function get_top_five_entries_by_categoryId_UserId($category_Id='0',$user_id='0') {
        $conditions = array('category_Id'=> $category_Id,'user_id'=> $user_id);
        $this->db->where($conditions);
        $query = $this->db->get('items', 5);
        return $query->result();
    }
 	function get_top_five_entries_by_categoryId_SessionId($category_Id='0',$session_id='0') {
        $conditions = array('category_Id'=> $category_Id,'session_id'=> $session_id);
        $this->db->where($conditions);
        $query = $this->db->get('items', 5);
	  $rowcount = $query->num_rows(); 
        if ($rowcount == 0){
		//add 5 item from default
		 $conditions = array('category_Id'=> $category_Id,'user_id'=> '-1');
	       $this->db->where($conditions);
      	 $query = $this->db->get('items', 5);
		//fetch and insert
		/*
		foreach ($query->result() as $row)
		{
    			echo $row->title;
		}*/	
		foreach ($query->result() as $categoryItem):
			//insert
			//print_r($categoryItem->name);
			
			$this->name = $categoryItem->name;
        		$this->company = $categoryItem->company;
        		$this->category_Id = $category_Id;
        		//if($uload_avata) $this->avatar=$uload_avata;
        		$this->session_id = $session_id;
        		$this->db->insert('items', $this);
			
		endforeach; 

		//reload again
		$conditions = array('category_Id'=> $category_Id,'session_id'=> $session_id);

		$this->db->where($conditions);
        	$query = $this->db->get('items', 5);	  
	  }
        return $query->result();
    }

     function next_five_entries_by_categoryId_SessionId($category_Id='0',$session_id='0', $item_index) {
        $conditions = array('category_Id'=> $category_Id,'session_id'=> $session_id);
        $this->db->where($conditions);
        $query = $this->db->get('items', $item_index + 4 , $item_index - 1 );
        return $query->result();
    }
     function prev_five_entries_by_categoryId_SessionId($category_Id='0',$session_id='0', $item_index) {
        $conditions = array('category_Id'=> $category_Id,'session_id'=> $session_id);
        //echo 'cat'.$category_Id;
        //echo 'ses'.$session_id;
        $this->db->where($conditions);
        //echo 'index'.($item_index +1).'';
        $query = $this->db->get('items', $item_index +6, $item_index+1);
        //echo 'idx='.$item_index.'.eidx' ;
        //print_r($query->result());
        return $query->result();
    }
    
    function insert_Item_For_Guest($session_id,$uload_avata=null) {
        $this->name = $this->input->post('name');
        $this->company = $this->input->post('company');
        $this->category_Id = $this->input->post('categoryId');
        if($uload_avata) $this->avatar=$uload_avata;
        $this->session_id = $session_id;
        $this->db->insert('items', $this);
    }
    function insert_Item_For_User($user_id,$uload_avata=null) {
        $this->name = $this->input->post('name');
        $this->company = $this->input->post('company');
        $this->category_Id = $this->input->post('categoryId');
        if($uload_avata) $this->avatar=$uload_avata;
        $this->user_id = $user_id;
        $this->db->insert('items', $this);
    }

    function update_Item() {
        $this->name = $this->input->post('name');
        $this->company = $this->input->post('company');
        $this->db->update('items', $this, array('id' => $this->input->post('id')));
    }
    function delete_Item_By_User_Id($user_id) {
        $this->db->where('user_id', $user_id);
        $this->db->delete('items'); 
    }
    
    function delete_Item($id) {
        $this->db->where('id', $id);
        $this->db->delete('items'); 
    }


}

?>
